// PROGRAMA : MSGPM001
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : EDU/FM DATA: 24/07/92
// OBJETIVO : ENVIO DE MENSAGENS ENTRE USUARIOS PELA MATRICULA
// OPCAO: 'MSG'
// ATUALIZACOES DATA
// PROGRAMA : MSGPM001 27/10/1997
// BIBLIOTECA : SISP
// PROGRAMADOR :
// OBJETIVO : ALTERCAO DE TELA E ACERTO DA DATAS (N6) PARA (N8)
// : ATUALIZAR USUARIOS 20/06/2002
// OBS : ESTE PROGRAMA FOI REESTRUTURADO POR SOLON MIRANDA
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import org.apache.commons.lang3.StringUtils;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Msgpm001A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Msgtm001;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Msgtm002;
import br.com.vti.sisp_sicrim.antigo.model.Sislm001;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Msgpm001 OBJETIVO : ENVIO DE MENSAGENS ENTRE USUARIOS PELA MATRICULA OPCAO: 'MSG' *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/msgpm001")
public class Msgpm001 {
	// Views utilizadas
	// Telas/relatorios utilizados
	Msgpm001A msgpm001A;
	Msgtm001 msgtm001;
	Msgtm002 msgtm002;
	// Includes utilizados
	Sislm001 sislm001 = new Sislm001();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Msgpm001 msgpm001Anterior;
	String faseproc = "0";
	// VARIAVEIS PARA BUSCA DE USER ATIVO

	private int retCode = 0;
	private int contAtivo = 0;
	private int len = 0;
	private int tidN = 0;
	private int tid = 0;
	private String area = "";
	// 02 USERID (A06)
	private String userDest = "";
	// 02 USER (A08/23)
	private String userMat = "";
	private String userNome = "";
	// VARIAVEIS PARA ENVIO DE MENSAGENS
	private int contOpc = 0;
	private int posicao = 0;
	private int contMsg = 0;
	private int lenght = 0;
	private String destino = "";
	private int quantTermb = 0;
	private String msgcb = "";
	private String mensagem = "";
	private String campo = "";
	private int dataDd = 0;
	private String b1 = "";
	private int dataMm = 0;
	private String b2 = "";
	private int dataAa = 0;
	private String dest = "";

	private Msgtm001 preMapas() {
		int ind;
		msgpm001A = new Msgpm001A(true, sessao);
		msgtm001 = new Msgtm001(true, sessao);
		msgtm002 = new Msgtm002(true, sessao);
		for (ind = 1; ind <= 6; ind++) {
			msgtm001.setSelecao(ind, "");
		}
		for (ind = 1; ind <= 20; ind++) {
			msgtm002.setAtivo(ind, "");
			msgtm002.setUseratv(ind, "");
			msgtm002.setTraco(ind, "");
			msgtm002.setOpc(ind, "");
			msgtm002.setNumero(ind, 0);
		}
		msgtm001.setNomeClasse(this.getClass().getSimpleName().toUpperCase());
		msgtm001.setDataCorrente(UtilBean.inverteDataCorrente());

		// MONTA CALENDARIO DO MES
		funcoes.sisnc001();
		msgtm001.setMes(funcoes.mes);
		msgtm001.setMesa(funcoes.dataL);
		return msgtm001;
	}

	public Msgpm001(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Msgpm001A msgpm001A, Msgtm001 msgtm001, Msgtm002 msgtm002) {
		try {
			main(msgpm001A, msgtm001, msgtm002);
		} catch (Exception e) {
			throw new ExceptionProgram(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Msgpm001A pmsgpm001A, Msgtm001 pmsgtm001, Msgtm002 pmsgtm002) {
		if (pmsgtm001 == null && pmsgtm002 == null && pmsgpm001A == null) {
			primeiraVez = true;
			// msgtm001 = new Msgtm001 (primeiraVez);
			msgtm001 = preMapas();
			msgtm001.setNavegar("../msgpm001/executar");
			result.include("msgtm001", msgtm001);
			sessao.setAtributo("MSGPM001", this);
			msgtm001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(pmsgpm001A, pmsgtm001, pmsgtm002);
		}

		switch (faseproc) {
		case "0": {
			fase0(pmsgtm002);
			break;
		}
		case "1": {
			fase1();
			break;
		}
		}
	}

	public void fase0(Msgtm002 pmsgtm002) {
		for (int i1 = 1; i1 <= 20; i1++) {
			msgtm002.setUseratv(i1, "");
			msgtm002.setNumero(i1, 0);
			msgtm002.setTraco(i1, "");
			msgtm002.setAtivo(i1, "");
		}

		msgtm001.receberTela(false, result);
		switch (msgtm001.getCamtela()) {
		case 9: {

			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S113590 S113590 S0819151 S0663773 S0310318 S0695908S0641142S1010628S0635516 S0691873 S0863191 S0916890S0609883S1120112S1135615";
			userMat = "S113590 S113590 S0819151 S0663773 S0310318 S0695908S0641142S1010628S0635516 S0691873 S0863191 S0916890S0609883S1120112S1135615";
			break;
		}
		case 11: {

			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		case 13: {

			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		case 15: {
			faseproc = "1";
			sessao.setAtributo("MSGPM001", this);
			result.include("msgpm001A", msgpm001A);
			msgpm001A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		case 17: {

			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		case 19: {

			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		default: {
			break;
		}
		}
		posicao = userDest.indexOf('U');
		// BUSCA DE USUARIOS ATIVOS

		len = 148;
		// QUANT-TID 64 = D 590 = P
		for (tidN = 1; tidN <= 590; tidN++) {

			tid = tidN;
			// QUANT-TID
			retCode = funcoes.getchr(area, len, tid);
			for (contAtivo = 1; contAtivo <= posicao; contAtivo++) {
				msgtm002.setTraco(contAtivo, "-");
				msgtm002.setNumero(contAtivo, contAtivo);
				msgtm002.setUseratv(contAtivo, UtilBean.subStr(userNome, 0 + contAtivo - 1, 8));
				if (UtilBean.subStr(area, 81, 8) == UtilBean.subStr(userMat, 0 + contAtivo - 1, 8)) {
					msgtm002.setAtivo(contAtivo, "*");
				}
			}
		}
		// ENVIO DE MENSAGENS
		for (int i1 = 1; i1 <= 20; i1++) {
			msgtm002.setOpc(i1, "");
		}
		for (int i1 = 1; i1 <= 20; i1++) {
			dest = UtilBean.moveSubstring(dest, 0 + i1 - 1, 1, "A", " ");
		}
		dest = "";

		if (!msgtm002.getEnviada()) {
			sessao.setAtributo("MSGPM001", this);
			result.include("msgtm002", msgtm002);
			msgtm002.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (pmsgtm002 != null) {
			msgtm002.receberTela(false, result);
		}
		if (msgtm002.getOperacao().equals("CAD")) {
			Sispm001 sispm001 = new Sispm001(result, sessao);
			sispm001.executar(null, null, null);
			return;
		}
		for (contOpc = 1; contOpc <= posicao; contOpc++) {
			if (!msgtm002.getOpc(contOpc).trim().equals("")) {
				dest = UtilBean.moveSubstring(dest, 0 + contOpc - 1, 8, "A", UtilBean.subStr(userDest, 0 + contOpc - 1, 10));
			}
		}
		// dest = UtilBean.concatena(UtilBean.subStr(dest,0, 8)[*]);
		posicao = 0;
		posicao = dest.indexOf(' ');
		posicao = (posicao - 1) / 8;
		if (dest.trim().equals("")) {
			Sispm001 sispm001 = new Sispm001(result, sessao);
			sispm001.executar(null, null, null);
			return;
		}

		destino = dest;

		quantTermb = posicao;
		campo = UtilBean.moveSubstring(campo, 0, 21, "A", " ORIGEM DA MENSAGEM: ");
		campo = UtilBean.moveSubstring(campo, 12, 18, "A", UtilBean.getInitUser());
		campo = UtilBean.moveSubstring(campo, 39, 7, "A", " DATA  ");
		dataDd = (int) UtilBean.extraiDigitos(msgtm001.getDataCorrente(), 8, 7, 2);
		b1 = "/ ";
		dataMm = (int) UtilBean.extraiDigitos(msgtm001.getDataCorrente(), 8, 5, 2);
		b2 = "/ ";
		dataAa = (int) UtilBean.extraiDigitos(msgtm001.getDataCorrente(), 8, 1, 4);
		campo = UtilBean.moveSubstring(campo, 56, 9, "A", " HORA   ");
		campo = UtilBean.moveSubstring(campo, 80, 80, "A", StringUtils.repeat("=", 80));
		msgcb = UtilBean.moveSubstring(msgcb, 0, 1, "A", "L");
		msgcb = UtilBean.moveSubstring(msgcb, 1, 1, "A", " ");
		msgcb = UtilBean.moveSubstring(msgcb, 3, 2, "A", "\0002");
		lenght = 0400;
		for (contMsg = 1; contMsg <= 2; contMsg++) {
			// PENDENCIA CHAMADA COBOL
			// mesgsw(rc ,msgcb ,mensagem ,lenght ,destino ,quantTermb); // CALL
			// ���
		}
		for (int i1 = 1; i1 <= 20; i1++) {
			msgtm002.setUseratv(i1, "");
		}
		sessao.setAtributo("MSGPM001", this);
		result.include("msgtm001", msgtm001);
		msgtm001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	public void fase1() {

		msgpm001A.receberTela(false, result);
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Sispm001 sispm001 = new Sispm001(result, sessao);
			sispm001.executar(null, null, null);
			return;
		}
		switch (msgpm001A.getCamtela()) {
		case 2: {
			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		case 5: {
			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		case 6: {
			userDest = "U=S113590 U=S113590 U=S0819151U=S0663773U=S0310318U=S0695908U=S0641142U=S1010628U=S0635516U=S0691873U=S0863191U=S0916890U=S0609883U=S1120112U=S1135615";
			userNome = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			userMat = "S1135904S113590 S0819151S0663773S0310318S0695908S0641142S1010628S0635516S0691873S0863191S0916890S0609883S1120112S1135615";
			break;
		}
		default: {
			msgtm001.enviarMensagem(null, "CURSOR  POSICIONADO INCORRETAMENTE");
			return;
		}
		}
	}

	private void restaurarEstados(Msgpm001A pmsgpm001A, Msgtm001 pmsgtm001, Msgtm002 pmsgtm002) {
		this.msgpm001Anterior = (Msgpm001) sessao.getAtributo("MSGPM001");
		if (pmsgpm001A != null) {
			msgpm001A = pmsgpm001A;
		} else {
			msgpm001A = this.msgpm001Anterior.msgpm001A;
		}
		msgpm001A.setSessao(sessao);
		if (pmsgtm001 != null) {
			msgtm001 = pmsgtm001;
		} else {
			msgtm001 = this.msgpm001Anterior.msgtm001;
		}
		msgtm001.setSessao(sessao);
		if (pmsgtm002 != null) {
			msgtm002 = pmsgtm002;
		} else {
			msgtm002 = this.msgpm001Anterior.msgtm002;
		}
		msgtm002.setSessao(sessao);

		// MONTA CALENDARIO DO MES
		funcoes.sisnc001();
		msgtm001.setMes(funcoes.mes);
		msgtm001.setMesa(funcoes.dataL);
		this.faseproc = this.msgpm001Anterior.faseproc;
	}
}
